package com.infopulse;

/**
 * Created by sky_lady on 5/19/14.
 */
public class QuickSort {

    public static void sort(int[] values) {

        if (values == null || values.length == 0) {
            return;
        }
        int[] array;
        int arLength = values.length;
        quicksort(values, 0, arLength - 1);
    }

    private static void quicksort(int[] array, int low, int high) {
        int i = low, j = high;
        int middle = array[low + (high - low) / 2];

        while (i <= j) {
            while (array[i] < middle) {
                i++;
            }
            while (array[j] > middle) {
                j--;
            }
            if (i <= j) {
                exchange(array, i, j);
                i++;
                j--;
            }
        }
        if (low < j)
            quicksort(array, low, j);
        if (i < high)
            quicksort(array, i, high);
    }

    private static void exchange(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}